﻿/*********************************************************************************
**                                                                              **
**  Copyright (C) 2024 LiLong                                                   **
**  This file is part of GnssLogs.                                              **
**                                                                              **
**  GnssLogs is free software: you can redistribute it and/or modify            **
**  it under the terms of the GNU Lesser General Public License as published by **
**  the Free Software Foundation, either version 3 of the License, or           **
**  (at your option) any later version.                                         **
**                                                                              **
**  GnssLogs is distributed in the hope that it will be useful,                 **
**  but WITHOUT ANY WARRANTY; without even the implied warranty of              **
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               **
**  GNU Lesser General Public License for more details.                         **
**                                                                              **
**  You should have received a copy of the GNU Lesser General Public License    **
**  along with GnssLogs.  If not, see <https://www.gnu.org/licenses/>.          **
**********************************************************************************/
#pragma once

#include "../GST.h"
#include "Commons.h"

namespace GnssLogs
{
namespace GSTDecoder
{
using namespace tao::pegtl;

// clang-format off
using LogHeader = NMEA_TALKER_IDENTIFIER("GST");
struct Rms :  Floating {};
struct Smjr : Floating {};
struct Smnr : Floating {};
struct Orient : Floating {};
struct Lat : Floating {};
struct Lon : Floating {};
struct Alt : Floating {};
// clang-format on
using Expression = seq<LogHeader,
                       Split,
                       opt<Utc>,
                       Split,
                       opt<Rms>,
                       Split,
                       opt<Smjr>,
                       Split,
                       opt<Smnr>,
                       Split,
                       opt<Orient>,
                       Split,
                       opt<Lat>,
                       Split,
                       opt<Lon>,
                       Split,
                       opt<Alt>,
                       one<'*'>,
                       CheckSum,
                       Tail>;

template<typename T>
struct Action
{
};

template<>
struct Action<LogHeader>
{
    template<typename ParseInput>
    static bool apply(ParseInput& in, GST::Data& gpgst)
    {
        gpgst.header = std::string(in.begin(), in.size());
        return true;
    }
};

template<>
struct Action<Utc> : public UTCHelperNoSetValue
{
};

template<>
struct Action<Rms> : public DoubleHelper<Action<Rms>>
{
    static inline void setValue(GST::Data& gpgst, double value) { gpgst.rms = value; }
};

template<>
struct Action<Smjr> : public DoubleHelper<Action<Smjr>>
{
    static inline void setValue(GST::Data& gpgst, double value) { gpgst.smjr = value; }
};

template<>
struct Action<Smnr> : public DoubleHelper<Action<Smnr>>
{
    static inline void setValue(GST::Data& gpgst, double value) { gpgst.smnr = value; }
};

template<>
struct Action<Orient> : public DoubleHelper<Action<Orient>>
{
    static inline void setValue(GST::Data& gpgst, double value) { gpgst.orient = value; }
};

template<>
struct Action<Lat> : public DoubleHelper<Action<Lat>>
{
    static inline void setValue(GST::Data& gpgst, double value) { gpgst.lat = value; }
};

template<>
struct Action<Lon> : public DoubleHelper<Action<Lon>>
{
    static inline void setValue(GST::Data& gpgst, double value) { gpgst.lon = value; }
};

template<>
struct Action<Alt> : public DoubleHelper<Action<Alt>>
{
    static inline void setValue(GST::Data& gpgst, double value) { gpgst.alt = value; }
};

template<>
struct Action<CheckSum> : CheckSumHelper<Action<CheckSum>>
{
    static inline void setValue(GST::Data& data, uint8_t value) { data.checkSum = value; }
};
} // namespace GSTDecoder
} // namespace GnssLogs